package com.it.sun.yuebike.framework.config;

import cn.dev33.satoken.interceptor.SaRouteInterceptor;
import cn.dev33.satoken.router.SaRouterUtil;
import cn.dev33.satoken.stp.StpUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @ClassName com.it.sun.yuebike.framework.config.SaTokenConfigure
 * @Author sunmannian
 * @Date 2021/5/31 0:01
 * @Version V1.0
 * Description  <sa-token配置类 >
 * Copyright (C) 北京中电兴发科技有限公司  since 2021/5/31
 */
//@Configuration
@Slf4j
@Data
public class SaTokenConfigure implements WebMvcConfigurer {

    @Autowired
    private Properties properties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaRouteInterceptor((request, response, handler) ->
                SaRouterUtil.match("/**", StpUtil::checkLogin)))
                .addPathPatterns("/**").excludePathPatterns(properties.excludeUrls);
    }

    @ConfigurationProperties(prefix = "spring.sa-token")
    @Component
    @Data
    public static class Properties {
        private List<String> excludeUrls;
    }
}



